angular.module('hyt.toolsService', ['ngResource'])

  .factory('GpsMap',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.savePrintSet,
      result;

    var resource = $resource(ApiUrl+':prints',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSavePrint : function(prints){
        console.log(prints);
        return resource.query({
          prints:prints
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('SavePrint.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('NoteService',function($resource, $rootScope,ENV,$ionicLoading,$state){
    var ApiUrl = ENV.postUrl+ENV.api.getNote,noteData={},pageSize=10;
    var resource = $resource(ApiUrl+':param',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getNoteData : function(param){
        var hasNextPage = true;
        resource.query({
          param:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            userMsg:param.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            noteData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('NoteList.Updated');
        })
      },
      getResult:function(){
        if(noteData[pageSize]===undefined){
          return false;
        }
        return noteData[pageSize].data;
      },
      getMoreNoteData : function(param){
        if(noteData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = noteData[pageSize].hasNextPage;
        var nextPage = noteData[pageSize].nextPage;
        var moreNoteData = noteData[pageSize].data;
        resource.query({
          param:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            userMsg:param.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreNoteData = moreNoteData.concat(r.rows);

          noteData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreNoteData
          };
          $rootScope.$broadcast('NoteList.Updated');
        })
      },
      hasNextPage: function() {
        if (noteData[pageSize] === undefined) {
          return false;
        }
        return noteData[pageSize].hasNextPage;
      }
    }
  })
  .factory('DeleteNote',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.deleteNote,
      result;

    var resource = $resource(ApiUrl+':paramDelete',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      deleteNote : function(paramDelete){
        return resource.query({
          paramDelete:paramDelete
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('DeleteNote.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })

  .factory('AddOrUpdateNote',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.addNote,
      result;

    var resource = $resource(ApiUrl);
    return{
      saveNote : function(paramAdd){
        return resource.save({
          noteInfo:{
            content:paramAdd.content,
            id:paramAdd.id
          },
          userMsg:paramAdd.userMsg
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddNote.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
